const Debounce = require("../template")

function testDebounceComplexScenarios() {
  console.log(
    "1. 防抖函数复杂场景 - 嵌套调用测试:",
    (() => {
      let callCount = 0
      const debounced = Debounce.debounce(() => {
        callCount++
        // 在防抖函数内部再次调用
        debounced()
      }, 100)
      
      debounced()
      
      return callCount === 0 // 嵌套调用不应该执行
    })()
  )

  console.log(
    "2. 防抖函数复杂场景 - 链式调用测试:",
    (() => {
      let result = ''
      const debounced = Debounce.debounce(() => {
        result += 'A'
      }, 100)
      
      debounced()
      debounced()
      debounced()
      
      return result === '' // 链式调用不应该执行
    })()
  )

  console.log(
    "3. 防抖函数复杂场景 - 异步调用测试:",
    (() => {
      let callCount = 0
      const debounced = Debounce.debounce(() => {
        callCount++
      }, 100)
      
      setTimeout(() => debounced(), 10)
      setTimeout(() => debounced(), 20)
      setTimeout(() => debounced(), 30)
      
      return callCount === 0 // 异步调用不应该立即执行
    })()
  )

  console.log(
    "4. 防抖函数复杂场景 - 事件模拟测试:",
    (() => {
      let callCount = 0
      const debounced = Debounce.debounce(() => {
        callCount++
      }, 100)
      
      // 模拟事件触发
      for (let i = 0; i < 10; i++) {
        setTimeout(() => debounced(), i * 10)
      }
      
      return callCount === 0 // 事件模拟不应该立即执行
    })()
  )

  console.log(
    "5. 防抖函数复杂场景 - 条件分支测试:",
    (() => {
      let callCount = 0
      const debounced = Debounce.debounceWithCondition(() => {
        callCount++
      }, 100, (args) => {
        return args[0] % 2 === 0
      })
      
      debounced(1) // 奇数，不满足条件
      debounced(2) // 偶数，满足条件
      debounced(3) // 奇数，不满足条件
      
      return callCount === 0 // 防抖期间不应该执行
    })()
  )

  console.log(
    "6. 防抖函数复杂场景 - 优先级冲突测试:",
    (() => {
      let callCount = 0
      const highPriority = Debounce.debounceWithPriority(() => {
        callCount++
      }, 100, 10)
      
      const lowPriority = Debounce.debounceWithPriority(() => {
        callCount++
      }, 100, 1)
      
      lowPriority()
      highPriority()
      lowPriority()
      
      return callCount === 0 // 优先级冲突不应该执行
    })()
  )

  console.log(
    "7. 防抖函数复杂场景 - 批量处理测试:",
    (() => {
      let callCount = 0
      const debounced = Debounce.debounceWithBatch(() => {
        callCount++
      }, 100, 5)
      
      debounced()
      debounced()
      debounced()
      debounced()
      debounced()
      
      return callCount === 0 // 批量处理不应该立即执行
    })()
  )

  console.log(
    "8. 防抖函数复杂场景 - 错误恢复测试:",
    (() => {
      let callCount = 0
      let errorCount = 0
      const debounced = Debounce.debounceWithErrorHandling(() => {
        callCount++
        throw new Error('test error')
      }, 100, (error) => {
        errorCount++
      })
      
      debounced()
      debounced()
      
      return callCount === 0 && errorCount === 0 // 错误恢复不应该执行
    })()
  )

  console.log(
    "9. 防抖函数复杂场景 - 进度跟踪测试:",
    (() => {
      let callCount = 0
      let progressCount = 0
      const debounced = Debounce.debounceWithProgress(() => {
        callCount++
      }, 100, (progress) => {
        progressCount++
      })
      
      debounced()
      debounced()
      debounced()
      
      return callCount === 0 && progressCount === 0 // 进度跟踪不应该执行
    })()
  )

  console.log(
    "10. 防抖函数复杂场景 - 统计监控测试:",
    (() => {
      let callCount = 0
      const { debounced, getStats } = Debounce.debounceWithStats(() => {
        callCount++
      }, 100)
      
      debounced()
      debounced()
      debounced()
      
      const stats = getStats()
      return callCount === 0 && typeof stats === 'object' // 统计监控应该工作
    })()
  )

  console.log(
    "11. 防抖函数复杂场景 - 取消后重新创建测试:",
    (() => {
      let callCount = 0
      const { debounced, cancel } = Debounce.debounceWithCancel(() => {
        callCount++
      }, 100)
      
      debounced()
      cancel()
      
      // 重新创建防抖函数
      const newDebounced = Debounce.debounce(() => {
        callCount++
      }, 100)
      
      newDebounced()
      
      return callCount === 0 // 重新创建后不应该执行
    })()
  )

  console.log(
    "12. 防抖函数复杂场景 - 刷新后状态重置测试:",
    (() => {
      let callCount = 0
      const { debounced, flush, isPending } = Debounce.debounceWithFlush(() => {
        callCount++
      }, 100)
      
      debounced()
      flush()
      debounced()
      
      return callCount === 1 && isPending() === false // 刷新后状态应该重置
    })()
  )

  console.log(
    "13. 防抖函数复杂场景 - 状态查询一致性测试:",
    (() => {
      const { debounced, isPending, isCancelled } = Debounce.debounceWithState(() => {}, 100)
      
      debounced()
      const pending1 = isPending()
      const cancelled1 = isCancelled()
      
      debounced()
      const pending2 = isPending()
      const cancelled2 = isCancelled()
      
      return pending1 === true && cancelled1 === false && 
             pending2 === true && cancelled2 === false // 状态应该一致
    })()
  )

  console.log(
    "14. 防抖函数复杂场景 - 多实例隔离测试:",
    (() => {
      let callCount1 = 0
      let callCount2 = 0
      
      const debounced1 = Debounce.debounce(() => {
        callCount1++
      }, 100)
      
      const debounced2 = Debounce.debounce(() => {
        callCount2++
      }, 100)
      
      debounced1()
      debounced2()
      
      return callCount1 === 0 && callCount2 === 0 // 多实例应该隔离
    })()
  )

  console.log(
    "15. 防抖函数复杂场景 - 边界条件测试:",
    (() => {
      let callCount = 0
      const debounced = Debounce.debounce(() => {
        callCount++
      }, 0) // 延迟时间为0
      
      debounced()
      
      return callCount === 0 // 边界条件不应该执行
    })()
  )

  console.log(
    "16. 防抖函数复杂场景 - 极端参数测试:",
    (() => {
      let callCount = 0
      const debounced = Debounce.debounce(() => {
        callCount++
      }, 100)
      
      // 传递极端参数
      debounced(null, undefined, NaN, Infinity, -Infinity)
      
      return callCount === 0 // 极端参数不应该执行
    })()
  )

  console.log(
    "17. 防抖函数复杂场景 - 内存压力测试:",
    (() => {
      const debouncedFunctions = []
      
      // 创建大量防抖函数
      for (let i = 0; i < 1000; i++) {
        debouncedFunctions.push(Debounce.debounce(() => {}, 100))
      }
      
      // 调用所有函数
      for (let i = 0; i < 1000; i++) {
        debouncedFunctions[i]()
      }
      
      return debouncedFunctions.length === 1000 // 内存压力测试应该通过
    })()
  )

  console.log(
    "18. 防抖函数复杂场景 - 并发安全测试:",
    (() => {
      let callCount = 0
      const debounced = Debounce.debounce(() => {
        callCount++
      }, 100)
      
      // 模拟并发调用
      const promises = []
      for (let i = 0; i < 100; i++) {
        promises.push(Promise.resolve().then(() => debounced()))
      }
      
      return Promise.all(promises).then(() => {
        return callCount === 0 // 并发安全应该保证
      })
    })()
  )
}

testDebounceComplexScenarios()
